package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.skyeye.event.properties.RedisStringStreamBean;
import com.fivefu.core.skyeye.event.services.MimirEventService;

import com.fivefu.core.skyeye.event.utils.OkHttpClientsUtils;
import com.fivefu.core.skyeye.event.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.*;

@Service
public class MimirEventServiceImpl implements MimirEventService {

    private Logger logger = LoggerFactory.getLogger(MimirEventServiceImpl.class);

    @Autowired
    private OkHttpClientsUtils okHttpClientsUtils;

    @Autowired
    private RedisStringStreamBean stringStreamBean;

    /**
     * 格式化数据
     * @param pageBean
     * @return
     */
    public <T> List<T> formatEventBean(MimirQueryPageBean pageBean, Class<T> tClass){
        List<T> list = new ArrayList<>();
        String[] columnArr = pageBean.getColumns();

        List<String[]> dataList = pageBean.getDatas();
        for (String[] dataArr : dataList) {
            Map<String,String> dataMap = new HashMap<>();
            for (int i = 0 ; i < columnArr.length ; i ++){
                String column = columnArr[i];
                String value = dataArr[i];

                dataMap.put(column, value);
            }
            T bean = JSON.parseObject(JSON.toJSONString(dataMap), tClass);
            list.add(bean);
        }
        return list;
    }

    /**
     * 查询事件信息(违停)
     * @param code 探头唯一标识
     */
    @Override
    public List<EventBean> queryMimirEventByMimirID(String code){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        String[] orderbys = new String[]{"start_time desc", "id"};
        String[] queryArr = new String[]{"id", "unique_code", "start_time", "end_time", "images"};
        Integer pageNo = 1, pageSize = 100;
        List<EventBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/illegal_event_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<EventBean> bean1 = formatEventBean(pageBean, EventBean.class);
            if (!bean1.isEmpty()){
                list.addAll(bean1);
            }else{
                break;
            }
            pageNo ++;
        }
        return list;
    }

    /**
     * 查询通用事件信息
     * @param code 探头唯一标识
     * @return
     */
    @Override
    public List<EventBean> queryCvMimirEventByMimirID(String code){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        String[] orderbys = new String[]{"start_time desc", "id"};
        String[] queryArr = new String[]{};
        Integer pageNo = 1, pageSize = 100;
        List<EventBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/cv_event_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<EventBean> bean1 = formatEventBean(pageBean, EventBean.class);
            if (!bean1.isEmpty()){
                list.addAll(bean1);
            }else{
                break;
            }
            pageNo ++;
        }
        return list;
    }

    /**
     * 查询信息
     * @param url 查询接口地址
     * @param conditions 查询条件
     * @param queryArr 查询字段
     * @param pageNo 页码
     * @param pageSize 数量
     */
    public MimirQueryPageBean queryMimirEvent(String url,List<MimirQueryConditionBean> conditions,String[] orderbys, String[] queryArr, Integer pageNo, Integer pageSize){
        if (StrUtils.isNull(stringStreamBean.getServiceUrl())){
            logger.error("${redis-stream.service_url} is null");
            return null;
        }
        JSONObject param = new JSONObject();
        param.put("opt_type","select");

        JSONObject queryParam = new JSONObject();
        queryParam.put("conditions",conditions);//条件
        queryParam.put("orderbys",orderbys);//排序
        queryParam.put("page_number",pageNo);//页码
        queryParam.put("page_size",pageSize);//数量
        queryParam.put("display_columns", queryArr);//查询字段

        param.put("request_data",queryParam);

        String paramStr = JSON.toJSONString(param);
        String result = okHttpClientsUtils.post(stringStreamBean.getServiceUrl() + url, paramStr);
        MimirQueryPageBean pageBean = null;
        if (StrUtils.isNotNull(result) && !result.equals("error")){
            MimirQueryResponseBean resultObj = JSON.parseObject(result,MimirQueryResponseBean.class);
            if (resultObj.getResult().equals("success")){
                pageBean = JSON.parseObject(resultObj.getData(),MimirQueryPageBean.class);
                
            }else{
                logger.error(result);
            }
        }
        if (pageBean == null){
            logger.error("url:" + url + ",param:" + paramStr + ",result:" + result);
        }
        return pageBean;
    }

    /**
     * 查询视频播放流
     * @return
     */
    @Override
    public List<StreamDataBean> queryCameraStream(){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        String[] orderbys = new String[]{"start_time desc", "id"};
        String[] queryArr = new String[]{"id", "created_time", "unique_code", "stream_url", "used"};
        Integer pageNo = 1, pageSize = 100;
        List<StreamDataBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/stream_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<StreamDataBean> bean = formatEventBean(pageBean, StreamDataBean.class);
            if (!bean.isEmpty()){
                list.addAll(bean);
            }else{
                break;
            }
            pageNo ++;
        }
        return list;
    }

    /**
     * 查询视频播放流
     * @param code
     * @return
     */
    public StreamDataBean queryCameraStream(String code){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        /*MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("used");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal("1");
        conditions.add(bean);*/

        MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
        bean1.setCol("unique_code");//字段名
        bean1.setOpt("=");//条件//值
        bean1.setVal(code);
        conditions.add(bean1);
        String[] orderbys = new String[]{"start_time desc", "id"};
        String[] queryArr = new String[]{"id", "created_time", "unique_code", "stream_url", "used"};
        MimirQueryPageBean pageBean = queryMimirEvent("/stream_data",conditions,orderbys,queryArr, 1, 1);
        List<StreamDataBean> list = formatEventBean(pageBean, StreamDataBean.class);

        if (list.size() > 0){
            StreamDataBean streamDataBean = list.get(0);
            return streamDataBean;
        }
        return null;
    }

    /**
     * 编辑视频流地址
     * @param type 操作类型(add,modify)
     * @param code 探头编号
     * @param used 是否启用
     */
    @Override
    public void editCameraStream(String type, String code,String url,Integer used){
        JSONObject editParam = new JSONObject();
        JSONObject modifyParam = new JSONObject();
        editParam.put("unique_code",code);//探头编号
        if (type.equals("add")){
            editParam.put("stream_url",url);//流地址
            editParam.put("used",1);//是否启用(新增默认启用)
        }else {
            modifyParam.put("stream_url",url);//流地址
            modifyParam.put("used",used);//是否启用
        }
        boolean flag = editMimirInfo("/stream_data", type, editParam, modifyParam);
        logger.info("编辑视频流地址：" + flag);
    }

    /**
     * 编辑违停识别信息
     * @param url 请求接口
     * @param type 操作类型
     * @param editParam 请求参数
     * @param modifyParam 修改参数
     * @return
     */
    public boolean editMimirInfo(String url, String type, JSONObject editParam, JSONObject modifyParam){
        if (StrUtils.isNull(stringStreamBean.getServiceUrl())){
            logger.error("${redis-stream.service_url} is null");
            return false;
        }
        JSONObject param = new JSONObject();
        param.put("opt_type",type);

        if (type.equals("modify")){
            editParam.put("modify_data",modifyParam);
        }
        param.put("request_data",editParam);
        String paramStr = JSON.toJSONString(param);
        String result = okHttpClientsUtils.post(stringStreamBean.getServiceUrl() + url, paramStr);
        if (StrUtils.isNotNull(result) && !result.equals("error")){
            JSONObject object = JSON.parseObject(result);
            if (object.getString("result").equals("success")){
                return true;
            }
        }
        logger.error("url:" + url + ",param:" + paramStr + ",result:" + result);
        return false;
    }

    /**
     * 查询视频切片信息
     * @return
     */
    public List<StreamSliceBean> queryCameraStreamSlice(){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        String[] orderbys = new String[]{"id"};
        String[] queryArr = new String[]{"id", "stream_id", "unique_code", "slices_time", "valid_time","node"};
        Integer pageNo = 1, pageSize = 100;
        List<StreamSliceBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/stream_slices_conf_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<StreamSliceBean> bean = formatEventBean(pageBean, StreamSliceBean.class);
            if (!bean.isEmpty()){
                list.addAll(bean);
            } else {
                break;
            }
            pageNo ++;
        }
        return list;
    }

    /**
     * 查询视频切片信息
     * @param code
     * @return
     */
    @Override
    public StreamSliceBean queryCameraStreamSlice(String code){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        String[] orderbys = new String[]{"id"};
        String[] queryArr = new String[]{"id", "stream_id", "unique_code", "slices_time", "valid_time","node"};
        MimirQueryPageBean pageBean = queryMimirEvent("/stream_slices_conf_data",conditions,orderbys,queryArr, 1, 1);
        List<StreamSliceBean> list = formatEventBean(pageBean, StreamSliceBean.class);

        if (list.size() > 0){
            StreamSliceBean streamDataBean = list.get(0);
            return streamDataBean;
        }
        return null;
    }

    /**
     * 编辑视频流切片信息
     * @param type 操作类型(add,modify)
     * @param code 探头唯一标识
     * @param node 节点
     * @param slicesTime 切片次数
     */
    @Override
    public void editCameraStreamSlice(String type, String code, String node, Integer slicesTime){
            JSONArray validTime = JSON.parseArray("[[\"0000\", \"2359\"]]");
            JSONObject editParam = new JSONObject();
            JSONObject modifyParam = new JSONObject();
            editParam.put("unique_code",code);//探头编号
            if (type.equals("add")){//新增
                editParam.put("slice_type","LF");//切片模式 LF:低频  HF:高频 暂时默认低频切
                editParam.put("slices_time",slicesTime);//切片次数
                editParam.put("valid_time",validTime);//切片有效时间
                editParam.put("node",node);//节点
                //ext_conf 拓展配置信息字段暂未设置值，后续若有使用可配置相关信息
            }else {
                modifyParam.put("slices_time",slicesTime);//切片次数
                modifyParam.put("valid_time",validTime);//切片有效时间
                modifyParam.put("node",node);
                modifyParam.put("slice_type","LF");//切片模式 LF:低频  HF:高频 暂时默认低频切
                //ext_conf 拓展配置信息字段暂未设置值，后续若有使用可配置相关信息
            }
            boolean flag = editMimirInfo("/stream_slices_conf_data", type, editParam, modifyParam);
            logger.info("编辑视频流切片信息：" + flag);

    }

    /**
     * 编辑视频流切片信息
     * @param type 操作类型(add,modify)
     * @param enable 是否启用(0:否 1:是)
     * @param code 探头唯一标识
     * @param discernmentBean 场景探头相关信息
     */
    @Override
    public void editCameraStreamSlice(String type, String code,Integer enable,DiscernmentBean discernmentBean){
        String node = discernmentBean.getSliceNodeName();//切片节点
        JSONArray validTime = JSON.parseArray(discernmentBean.getCameraValidTime());//探头切片有效时间
        //JSONArray validTime = JSON.parseArray("[[\"0000\", \"2359\"]]");//切片有效时间
        JSONObject editParam = new JSONObject();
        JSONObject modifyParam = new JSONObject();
        editParam.put("unique_code",code);//探头编号
        if (type.equals("add")){//新增
            editParam.put("slice_type","LF");//切片模式 LF:低频  HF:高频 暂时默认低频切
            editParam.put("slices_time",100);//切片次数
            editParam.put("valid_time",validTime);//切片有效时间
            editParam.put("node",node);//节点
            //ext_conf 拓展配置信息字段暂未设置值，后续若有使用可配置相关信息
        }else {
            modifyParam.put("slices_time",100);//切片次数
            modifyParam.put("valid_time",validTime);//切片有效时间
            if (enable.equals(0)){
                modifyParam.put("node","");
            }else if (enable.equals(1)){
                modifyParam.put("node",node);//节点
            }
            modifyParam.put("slice_type","LF");//切片模式 LF:低频  HF:高频 暂时默认低频切
            //ext_conf 拓展配置信息字段暂未设置值，后续若有使用可配置相关信息
        }
        boolean flag = editMimirInfo("/stream_slices_conf_data", type, editParam, modifyParam);
        logger.info("编辑视频流切片信息：" + flag);

    }
    /**
     * 查询视频切片信息
     * @return
     */
    @Override
    public List<IllegalCameraBean> queryIllegalCameraStream(Integer cvId){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        if (cvId != null){
            MimirQueryConditionBean bean = new MimirQueryConditionBean();
            bean.setCol("cv_id");//字段名
            bean.setOpt("=");//条件//值
            bean.setVal(cvId + "");
            conditions.add(bean);
        }

        String[] orderbys = new String[]{"id","created_time desc"};
        String[] queryArr = new String[]{"id", "stream_id", "unique_code", "illegal_pts", "illegal_valid",
                "illegal_time_duration", "illegal_valid_time", "used", "node"};
        Integer pageNo = 1, pageSize = 100;
        List<IllegalCameraBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/illegal_stream_cv_conf_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<IllegalCameraBean> bean = formatEventBean(pageBean, IllegalCameraBean.class);
            if (!bean.isEmpty()){
                list.addAll(bean);
            } else {
                break;
            }
            pageNo ++;
        }

        return list;
    }

    /**
     * 查询违停视频流信息
     * @param code
     * @return
     */
    @Override
    public IllegalCameraBean queryIllegalCameraStream(String code,Integer cvId){
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        if (cvId != null){
            MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
            bean1.setCol("cv_id");//字段名
            bean1.setOpt("=");//条件//值
            bean1.setVal(cvId + "");
            conditions.add(bean1);
        }
        String[] orderbys = new String[]{"id","created_time desc"};
        String[] queryArr = new String[]{"id", "stream_id", "unique_code", "illegal_pts", "illegal_valid",
                "illegal_time_duration", "illegal_valid_time", "used", "node"};
        MimirQueryPageBean pageBean = queryMimirEvent("/illegal_stream_cv_conf_data",conditions,orderbys,queryArr, 1, 1);
        List<IllegalCameraBean> list = formatEventBean(pageBean, IllegalCameraBean.class);

        if (list.size() > 0){
            IllegalCameraBean streamDataBean = list.get(0);
            return streamDataBean;
        }
        return null;
    }

    /**
     * 编辑违停识别配置
     * @param type 操作类型(add,modify)
     * @param code 探头编号
     * @param cvId 场景类型
     * @param used 是否启用
     * @param eventNode 识别节点
     * @param timeDuration 时长
     * @param pts 区域范围
     * @param cameraValidTime 探头有效时间
     */
    @Override
    public void editIllegalCameraStream(String type, String code, Integer cvId, Integer used, String eventNode, Integer timeDuration, JSONArray pts, JSONArray cameraValidTime) {
            JSONObject editParam = new JSONObject();
            JSONObject modifyParam = new JSONObject();
            editParam.put("unique_code",code);//探头编号
            editParam.put("cv_id",cvId);//场景类型
            if (type.equals("add")){//新增
                editParam.put("illegal_pts",pts);//区域范围
                editParam.put("illegal_duration",timeDuration);//时长
                editParam.put("illegal_valid_time",cameraValidTime);//探头有效时间
                editParam.put("node",eventNode);//识别节点
                editParam.put("used",1);
            }else {
                modifyParam.put("illegal_pts",pts);//区域范围
                modifyParam.put("illegal_time_duration",timeDuration);//时长
                modifyParam.put("illegal_valid_time",cameraValidTime);//探头有效时间
                modifyParam.put("node",eventNode);//识别节点
                modifyParam.put("used",used);
            }
            boolean flag = editMimirInfo("/illegal_stream_cv_conf_data", type, editParam, modifyParam);
            logger.info("编辑违停识别配置信息：" + flag);
    }

    @Override
    public void editIllegalCameraStream(String type, String code, Integer cvId, Integer used,DiscernmentBean discernmentBean) {
        JSONArray pts = new JSONArray();//区域范围
        //区域范围
        if (StrUtils.isNotNull(discernmentBean.getScopeArea())) {
            pts = JSON.parseArray(discernmentBean.getScopeArea());//区域范围
        }
        Long timeDuration = discernmentBean.getContinuedTime();//持续时长
        JSONArray cameraValidTime = JSON.parseArray(discernmentBean.getCameraValidTime());//探头识别时间
        String eventNode = discernmentBean.getEventNodeName();//识别节点

        JSONObject editParam = new JSONObject();
        JSONObject modifyParam = new JSONObject();
        editParam.put("unique_code",code);//探头唯一标识
        editParam.put("cv_id",cvId);//场景类型
        if (type.equals("add")){//新增
            editParam.put("illegal_pts",pts);//区域范围
            editParam.put("illegal_duration",timeDuration);//持续时长
            editParam.put("illegal_valid_time",cameraValidTime);//探头有效时间
            editParam.put("node",eventNode);//识别节点
            editParam.put("used",1);
        }else {
            modifyParam.put("illegal_pts",pts);//区域范围
            modifyParam.put("illegal_time_duration",timeDuration);//持续时长
            modifyParam.put("illegal_valid_time",cameraValidTime);//探头有效时间
            modifyParam.put("node",eventNode);//识别节点
            modifyParam.put("used",used);
        }
        boolean flag = editMimirInfo("/illegal_stream_cv_conf_data", type, editParam, modifyParam);
        logger.info("编辑违停识别配置信息：" + flag);
    }

    /**
     * 格式化画线信息
     * @param area
     * @return
     */
    @Override
    public List<List<Integer[]>> formatPoints(String area){
        List<List<Integer[]>> points = new ArrayList<>();
        if (StrUtils.isNotNull(area)) {
            List<JSONArray> list = JSON.parseArray(area, JSONArray.class);
            for (JSONArray array : list){
                List<Integer[]> arrList = new ArrayList<>();
                for (int i = 0 ; i < array.size() - 1 ; i ++){
                    JSONObject obj = array.getJSONObject(i);
                    Integer x = obj.getDouble("x").intValue();
                    Integer y = obj.getDouble("y").intValue();
                    Integer[] arr = new Integer[]{x,y};
                    arrList.add(arr);
                }
                if (arrList.size() > 0) {
                    points.add(arrList);
                }
            }
        }
        return points;
    }

    @Override
    public List<SceneDataBean> queryScene() {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{};
        Integer pageNo = 1, pageSize = 100;
        List<SceneDataBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/cv_scene_conf_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<SceneDataBean> bean = formatEventBean(pageBean, SceneDataBean.class);
            if (!bean.isEmpty()){
                list.addAll(bean);
            } else {
                break;
            }
            pageNo ++;
        }
        return list;
    }

    @Override
    public List<SceneDataBean> queryScene(String code) {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        /*MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
        bean1.setCol("used");//字段名
        bean1.setOpt("=");//条件//值
        bean1.setVal("1");
        conditions.add(bean1);*/
        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{};
        Integer pageNo = 1, pageSize = 100;
        List<SceneDataBean> list = new ArrayList<>();
        while(true){
            MimirQueryPageBean pageBean = queryMimirEvent("/cv_scene_conf_data",conditions,orderbys,queryArr, pageNo, pageSize);
            List<SceneDataBean> bean2 = formatEventBean(pageBean, SceneDataBean.class);
            if (!bean2.isEmpty()){
                list.addAll(bean2);
            } else {
                break;
            }
            pageNo ++;
        }
        return list;

    }

    @Override
    public SceneDataBean queryScene(Integer id) {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("id");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(id+"");
        conditions.add(bean);
        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{};
        MimirQueryPageBean pageBean = queryMimirEvent("/cv_scene_conf_data",conditions,orderbys,queryArr, 1, 1);
        List<SceneDataBean> list = formatEventBean(pageBean, SceneDataBean.class);

        if (list.size() > 0){
            SceneDataBean sceneDataBean = list.get(0);
            return sceneDataBean;
        }
        return null;
    }

    @Override
    public SceneDataBean queryScene(String code, Integer cvId) {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(code);
        conditions.add(bean);
        MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
        bean1.setCol("cv_id");//字段名
        bean1.setOpt("=");//条件//值
        bean1.setVal(cvId + "");
        conditions.add(bean1);
        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{};
        MimirQueryPageBean pageBean = queryMimirEvent("/cv_scene_conf_data",conditions,orderbys,queryArr, 1, 1);
        List<SceneDataBean> list = formatEventBean(pageBean, SceneDataBean.class);

        if (list.size() > 0){
            SceneDataBean sceneDataBean = list.get(0);
            return sceneDataBean;
        }
        return null;
    }

    /**
     * 编辑场景识别
     * @param type 操作类型(add,modify)
     * @param code 探头唯一标识
     * @param cvId 场景类型
     * @param used 是否启用(0:否 1:是)
     * @param eventNode 识别节点
     * @param timeDuration 时长
     * @param pts 区域范围
     * @param cameraValidTime 探头有效时间
     * @param cvParam 识别参数
     */
    @Override
    public void editScene(String type,  String code, Integer cvId, Integer used, String eventNode, Integer timeDuration, JSONArray pts, JSONArray cameraValidTime, String cvParam) {
        JSONObject editParam = new JSONObject();
        JSONObject modifyParam = new JSONObject();
        JSONObject object = new JSONObject();
        if (StrUtils.isNotNull(cvParam)){
            object = JSON.parseObject(cvParam);
            }
        editParam.put("cv_id",cvId);//场景类型
        editParam.put("unique_code",code);//探头编号
        if (type.equals("add")){//新增
            editParam.put("cv_pts",pts);//区域范围
            editParam.put("cv_time_duration",timeDuration);//时长
            editParam.put("cv_valid_time",cameraValidTime);//探头有效时间
            editParam.put("cv_scene_conf",object);
            editParam.put("node",eventNode);//识别节点
            editParam.put("used",1);//新增默认开启
        }else {
            modifyParam.put("cv_pts",pts);//区域范围
            modifyParam.put("cv_time_duration",timeDuration);//时长
            modifyParam.put("cv_valid_time",cameraValidTime);//探头有效时间
            modifyParam.put("cv_scene_conf",object);
            modifyParam.put("node",eventNode);//识别节点
            modifyParam.put("used",used);
        }
        boolean flag = editMimirInfo("/cv_scene_conf_data", type, editParam, modifyParam);
        logger.info("编辑场景探头信息：" + flag);
    }

    /**
     * 编辑场景识别
     * @param type 操作类型(add,modify)
     * @param code 探头唯一标识
     * @param cvId 场景类型
     * @param used 是否启用(0:否 1:是)
     * @param discernmentBean 场景探头相关信息
     */
    @Override
    public void editScene(String type,String code, Integer cvId, Integer used,DiscernmentBean discernmentBean) {
        JSONArray cameraValidTime = JSON.parseArray(discernmentBean.getCameraValidTime());//探头有效时间
        String cvParam = discernmentBean.getCvParam();//探头识别参数
        JSONArray pts = new JSONArray();//区域范围
        //区域范围
        if (StrUtils.isNotNull(discernmentBean.getScopeArea())) {
            pts = JSON.parseArray(discernmentBean.getScopeArea());//区域范围
        }
        Long timeDuration = discernmentBean.getContinuedTime();//持续时长
        String eventNode = discernmentBean.getEventNodeName();//识别节点
        JSONObject editParam = new JSONObject();
        JSONObject modifyParam = new JSONObject();
        JSONObject object = new JSONObject();
        if (StrUtils.isNotNull(cvParam)){
           object = JSON.parseObject(cvParam);
        }
        editParam.put("cv_id",cvId);//场景类型
        editParam.put("unique_code",code);//探头编号
        if (type.equals("add")){//新增
            editParam.put("cv_pts",pts);//区域范围
            editParam.put("cv_time_duration",timeDuration);//持续时长
            editParam.put("cv_valid_time",cameraValidTime);//探头有效时间
            editParam.put("cv_scene_conf",object);
            editParam.put("node",eventNode);//识别节点
            editParam.put("used",1);//新增默认开启
        }else {
            modifyParam.put("cv_pts",pts);//区域范围
            modifyParam.put("cv_time_duration",timeDuration);//持续时长
            modifyParam.put("cv_valid_time",cameraValidTime);//探头有效时间
            modifyParam.put("cv_scene_conf",object);
            modifyParam.put("node",eventNode);//识别节点
            modifyParam.put("used",used);
        }
        boolean flag = editMimirInfo("/cv_scene_conf_data", type, editParam, modifyParam);
        logger.info("编辑场景探头信息：" + flag);
    }

    /**
     * 视频切片日志数据查询
     * @param uniqueCode   探头唯一编码
     * @param stime 开始时间(yyyyMMddHHmmss)
     * @param etime 结束时间(yyyyMMddHHmmss)
     * @param pageNo 页码
     * @param pageSize 数量
     * @return
     */
    @Override
    public List<VideoCaptureBean> queryVideoCapture(String uniqueCode, String stime, String etime, Integer pageNo, Integer pageSize) {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(uniqueCode);
        conditions.add(bean);

        if (StrUtils.isNotNull(stime)){
            MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
            bean1.setCol("created_time");//字段名
            bean1.setOpt(">");//条件//值
            bean1.setVal(stime);
            conditions.add(bean1);
        }

        if (StrUtils.isNotNull(etime)){
            MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
            bean1.setCol("created_time");//字段名
            bean1.setOpt("<");//条件//值
            bean1.setVal(etime);
            conditions.add(bean1);
        }

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 12;
        }

        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{"id","created_by","created_time","updated_by","updated_time",
                                        "is_delete","stream_id","unique_code","status","message"};
        MimirQueryPageBean pageBean = queryMimirEvent("/stream_slices_log_data",conditions,orderbys,queryArr, pageNo, pageSize);
        List<VideoCaptureBean> videoCaptureBeans = formatEventBean(pageBean, VideoCaptureBean.class);
        return videoCaptureBeans;
    }

    /**
     * 视频切片数据查询    视频截图信息查询
     * @param uniqueCode   探头唯一编码
     * @param stime 开始时间(yyyyMMddHHmmss)
     * @param etime 结束时间(yyyyMMddHHmmss)
     * @param pageNo 页码
     * @param pageSize 数量
     * @return
     */
    @Override
    public List<StreamSlicesDataBean> getStreamSlicesData(String uniqueCode, String stime, String etime, Integer pageNo, Integer pageSize) {
        List<MimirQueryConditionBean> conditions = new ArrayList<>();
        MimirQueryConditionBean bean = new MimirQueryConditionBean();
        bean.setCol("unique_code");//字段名
        bean.setOpt("=");//条件//值
        bean.setVal(uniqueCode);
        conditions.add(bean);

        if (StrUtils.isNotNull(stime)){
            MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
            bean1.setCol("created_time");//字段名
            bean1.setOpt(">");//条件//值
            bean1.setVal(stime);
            conditions.add(bean1);
        }

        if (StrUtils.isNotNull(etime)){
            MimirQueryConditionBean bean1 = new MimirQueryConditionBean();
            bean1.setCol("created_time");//字段名
            bean1.setOpt("<");//条件//值
            bean1.setVal(etime);
            conditions.add(bean1);
        }

        if (pageNo == null){
            pageNo = 1;
        }
        if (pageSize == null){
            pageSize = 12;
        }

        String[] orderbys = new String[]{"id", "created_time desc"};
        String[] queryArr = new String[]{"id","created_by","created_time","updated_by","updated_time",
                "is_delete","stream_id","unique_code","image_path","image_type"};
        MimirQueryPageBean pageBean = queryMimirEvent("/stream_slices_data",conditions,orderbys,queryArr, pageNo, pageSize);
        List<StreamSlicesDataBean> beans = formatEventBean(pageBean, StreamSlicesDataBean.class);
        return beans;
    }
}
